@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {
    @Resource
    private EmployeeService employeeService;

    /**
     * 1.处理employee登录方法
     * @param employee 员工信息
     * @return Result对象
     */
    @PostMapping("/login")
    public Result login(@RequestBody Employee employee){
        //1.获取页面提交的用户名和密码
        String username = employee.getUsername();
        String password = employee.getPassword();
        //对传递过来的密码进行md5加密
//        password = DigestUtils.md5DigestAsHex(password.getBytes());//测试 todo

        //2.根据用户名username查询数据库
        String token = employeeService.authenticateUser(username, password);

        return Result.success(token);
    }

    /**
     * 处理新增员工
     * @param employee 员工信息
     * @return Result对象
     */
    @PostMapping("/save")
    public Result save(@RequestBody Employee employee){
        employeeService.saveEmployee(employee);
        return Result.success("新增员工成功");
    }

    /**
     * 处理分页查询
     * @param pageNo 当前页数
     * @param pageSize 每页显示条数
     * @param search 搜索框内容
     * @return Result对象
     */
    @GetMapping("/page")
    public Result page(@RequestParam(value = "pageNo",defaultValue = "1") Integer pageNo,
                       @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                       @RequestParam(value = "search",required = false,defaultValue = "") String search){
        //1.分页查询
        LambdaQueryWrapper<Employee> queryWrapper = Wrappers.lambdaQuery();
        if(StringUtils.hasText(search)){
            queryWrapper.like(Employee::getName,search);
        }
        Page<Employee> page = employeeService.page(new Page<>(pageNo, pageSize), queryWrapper);
        return Result.success(page);
    }

    /**
     * 处理employee修改方法
     * 修改员工账号状态和员工信息 合二为一
     * @param employee 员工信息
     * @return Result对象
     */
    @PutMapping("/update")
    public Result update(@RequestBody Employee employee){
        employeeService.updateById(employee);
        return Result.success("修改员工信息成功");
    }
}
